<?php

class PhpDocGtk {
	
	/**
	 * The registry
	 * 
	 * @var Zend_Registry
	 */
	protected static $_registry;
	
	/**
	 * The translate object.
	 * 
	 * @var Zend_Translate
	 */
	protected static $_translate;
	
	/**
	 * The config object.
	 * 
	 * @var Zend_Config
	 */
	protected static $_config;
	
	/**
	 * Returns the registry.
	 * 
	 * @return Zend_Registry
	 */
	public static function registry() {
		if(empty(self::$_registry) || !self::$_registry instanceof Zend_Registry) {
			self::$_registry = Zend_Registry::getInstance();
		}
		return self::$_registry;
	}
	
	/**
	 * Returns the configuration object.
	 * 
	 * @return Zend_Config
	 */
	public static function config() {
		if(empty(self::$_config) || !self::$_config instanceof Zend_Config) {
			$sessionFile = realpath('../config/session.tmp');
			if(!file_exists($sessionFile)) {
				if(!touch($sessionFile)) {
					throw new PhpDocGtk_Exception('Session file couldn\'t be created: ' . $sessionFile);
				}
			}
			$config = unserialize(file_get_contents($sessionFile));
			if(empty($config)) {
				$config = array('projects' => array('dir' => null,
											'recent' => null),
								'user' => array('lang' => 'en'));
			}
			self::$_config = new Zend_Config($config);
		}
		return self::$_config;
	}
	
	public static function run() {
		
		Gtk::main();
	}
	
	/**
	 * Translate the given string.
	 * 
	 * @param string $string
	 * @return string
	 * @uses Zend_Translate
	 */
	public static function t($string) {
		if(empty(self::$_translate) || !self::$_translate instanceof Zend_Translate) {
			self::$_translate = new Zend_Translate('csv',realpath('../lang/en.csv'),'en',array('separator' => '|'));
			if(self::$_config->user-lang != 'en') {
				$newLangFile = realpath('../lang/' . self::config()->user->lang . '.csv');
				if(file_exists($newLangFile)) {
					self::$_translate->addTranslation('csv',realpath('../lang/' . self::config()->user->lang . '.csv'),self::config()->user->lang,array('separator' => '|'));
				}
			}
		}
		return self::$_translate->_($string);
	}
}
?>